

#import "PayViewController.h"
#import <Masonry/Masonry.h>
#import "ResponseManager.h"
#import <AlipaySDK/AlipaySDK.h>
#import "FBProgressView.h"


@interface PayViewController ()
{
    UITextField *textField;
    UIButton *affirmButton;
}

@end

@implementation PayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"打赏";
    [self setupBackItem];
    [self setUpSubView];
    [self buttonColor];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(buttonColor) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)setUpSubView {
    
    textField = [[UITextField alloc]init];
    textField.backgroundColor = [UIColor whiteColor];
    textField.borderStyle = UITextBorderStyleNone;
    textField.placeholder = @"请输入金额";
    textField.textAlignment = NSTextAlignmentRight;
    textField.font = [UIFont systemFontOfSize:13];
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 40)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 60, 40)];
    label.text = @"打赏金额";
    label.font = [UIFont systemFontOfSize:13];
    [leftView addSubview:label];
    
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    UIView *lin = [[UIView alloc]initWithFrame:CGRectMake(5, 13, 1, 15)];
    lin.backgroundColor = RGBCOLOR(200.0f, 200.0f, 200.0f, 1);
    UILabel *rightlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 20, 40)];
    rightlabel.text = @"元";
    rightlabel.font = [UIFont systemFontOfSize:13];
    
    [rightView addSubview:lin];
    [rightView addSubview:rightlabel];
    
    textField.leftView = leftView;
    textField.rightView = rightView;

    [textField setLeftViewMode:UITextFieldViewModeAlways];
    [textField setRightViewMode:UITextFieldViewModeAlways];
    
    [self.view addSubview:textField];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(74);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
        
    }];
    
    
    UITextField *alipay = [[UITextField alloc]init];
    
    alipay.backgroundColor = [UIColor whiteColor];
    alipay.borderStyle = UITextBorderStyleNone;
    alipay.secureTextEntry = YES;
    alipay.font = [UIFont systemFontOfSize:13];
    
    UIView *left = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 140, 40)];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20, 8, 25, 25)];
    image.image = [UIImage imageNamed:@"愿望-支付宝"];
    UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(image.frame.origin.x + 35, 0, 80, 40)];
    text.font = [UIFont systemFontOfSize:13];
    text.text = @"支付宝支付";
    
    [left addSubview:image];
    [left addSubview:text];
    
    [self.view addSubview:alipay];
    
    
    UIView *right = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
    UIImageView *rightImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 20, 20)];
    rightImage.image = [UIImage imageNamed:@"愿望-打赏-选中"];
    [right addSubview:rightImage];
    
    alipay.leftView = left;
    alipay.rightView = right;
    
    [alipay setRightViewMode:UITextFieldViewModeAlways];
    [alipay setLeftViewMode:UITextFieldViewModeAlways];
    
    
    [alipay mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(textField.mas_bottom).with.offset(70);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
        
    }];
    
    UIButton *alipayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [alipay addSubview:alipayButton];
    
    [alipayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.height.equalTo(alipay);
    }];
    
    alipayButton.tag = 600;

    
    [alipayButton addTarget:self action:@selector(selectedButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    affirmButton= [[UIButton alloc]init];
    affirmButton.backgroundColor = RGBCOLOR(170.0f, 170.0f, 170.0f, 1);
    [affirmButton setTitle:@"确认打赏" forState:UIControlStateNormal];
    affirmButton.titleLabel.textColor = [UIColor whiteColor];
    affirmButton.titleLabel.font = [UIFont systemFontOfSize:15];
    affirmButton.layer.cornerRadius = 5;
    [self.view addSubview:affirmButton];
    
    [affirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(alipay.mas_bottom).with.offset(100);
        make.height.mas_equalTo(40);
        
    }];

    
    
    
    
}

- (void)buttonColor {
    
    if (textField.text.length >= 1) {
        
        affirmButton.backgroundColor = BLUE_COLOR;
        [affirmButton setTitleColor:RGBCOLOR(205.0f, 205.0f, 205.0f, 1) forState:UIControlStateHighlighted];
        [affirmButton addTarget:self action:@selector(affirmButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    if (textField.text.length == 0){
        affirmButton.backgroundColor = RGBCOLOR(170.0f, 170.0f, 170.0f, 1);

    }
    
}


- (void)affirmButtonTaped:(UIButton *)sender {
    NSDictionary *parameters = @{@"bonus":textField.text,@"wish_id":self.wishID,@"pay_type":@(1)};
    [FBProgressView showWithMessage:nil];
    [ResponseManager startPostRequestWithURLString:kRewardWishURL parameters:parameters completionHandler:^(FBResponse *response) {
        
        [FBProgressView hide];
        NSString *orderInfo = response.resultValue[@"code"];
        [[AlipaySDK defaultService] payOrder:orderInfo fromScheme:@"AHLZBQ" callback:^(NSDictionary *resultDic) {
            
            NSLog(@"支付结果 = %@",resultDic);

        }];

    }];
    
    
    
    
    
}

- (void)selectedButtonTaped:(UIButton *)sender {
    
    
}



@end
